<?php

namespace App\Http\Controllers;


use App\Models\Article;
use App\Models\Category;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    // 首页
    public function index(Request $request)
    {
        $page = $request->query('page', 1);
        $limit = $request->query('limit', 7);
        // 首页展示文章
        $articles = Article::select('*')
            ->orderBy('created_at', 'desc')
            ->skip(($page - 1) * $limit)
            ->take($limit)->get();

        // 最新文章
        $latelys = Article::select()->orderBy('updated_at', 'desc')->take(4)->get();

        // 所有分类
        $cates = Category::select('id', 'name')->withCount('articles')->orderBy('articles_count', 'desc')->get();

        return view('index', ['articles' => $articles, 'latelys' => $latelys, 'cates' => $cates]);
    }
}
